app.controller('chainGoodsAddCtrl', ['$scope', '$state', 'utilService', 'chainGoodsAddService', 'toaster', function ($scope, $state, utilService, chainGoodsAddService, toaster) {

    $scope.chainGoodsAddService = chainGoodsAddService;
    $scope.chainGoodsListVO = {
        status: 1
    };

    //查询商品列表
    $scope.findPageAll = function () {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + '/chain/goods/id',
            params: $scope.chainGoodsListVO
        }).then(function (result) {
        });
    }

    $scope.findPageAll();

    //导入商品接口
    $scope.importState = false;

    $scope.importGoods = function (id) {
        if ($scope.importState) {
            return;
        }

        $scope.importState = true;

        chainGoodsAddService.importGoods($scope, id).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.importState = false;
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findPageAll();
                    break;
                default :
                    $scope.importState = false;
                    break;
            }
        })
    }

    $scope.selected = [];
    $scope.checkAll = function ($event) {
        var checkbox = $event.target;
        var checked = checkbox.checked;
        for (var i = 0; i < $scope.list.length; i++) {
            if(checked) {
                if (!$scope.importState) {
                    $scope.selected.push($scope.list[i]);
                }
            }else{
                $scope.selected.splice(0,$scope.selected.length);
            }
        }
    }
    $scope.isCheckedAll=function(){
        if($scope.list==null){
            return;
        }
        return $scope.selected.length==$scope.list.length;
    }
    $scope.isChecked = function (item) {
        return $scope.selected.indexOf(item)>=0;
    }
    $scope.batchImportGoods = function () {
        $scope.batchImportStatus=true;
        chainGoodsAddService.batchImportGoods($scope).then(
            function(result){
                switch (result.status) {
                    case 200:
                        $scope.importState = false;
                        $scope.batchImportStatus=false;
                        toaster.pop('success', "提示", "操作成功");
                        $scope.findPageAll();
                        break;
                    default :
                        $scope.importState = false;
                        $scope.batchImportStatus=false;
                        break;
                }
            }
        );
    }
}])


